package ${model.packageName}.controller.web.mis;

import java.util.Date;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.util.StringUtils;
import com.apa.biz.common.CustomRowBounds; 
import com.apa.biz.enums.ReturnCode;
import com.apa.biz.enums.SoftVersionType;
import com.apa.biz.exception.ServiceException;
import com.apa.biz.result.support.SimpleLayUIQueryResult;
import com.apa.biz.result.support.SimpleObjectResult; 
import com.apa.biz.utils.HandleMessyCode;
import com.apa.biz.utils.ServletsUtil;
import com.apa.controller.web.WebController;
import ${model.packageName}.${model.modulePackage}.service.${model.model}Service;
import ${model.packageName}.${model.modulePackage}.entity.${model.model}Entity; 
#foreach($field in $fieldList)
#if(${field.inputType}=="select"&&${field.inputValueOne})
import ${model.packageName}.${model.modulePackage}.service.${field.inputValueOneU}Service;
import com.apa.biz.entity.${field.inputValueOneU}Entity;
#end
#end

/**
*@Title: ${model.model}Controller
*@Description: ${model.comment} 的web控制层
**@author ${model.author} ${date}
*/
@Controller("${model.modelL}Controller")
@RequestMapping("/${model.modelL}")
public class ${model.model}Controller extends WebController{
    
    private static Logger logger = LoggerFactory.getLogger(${model.model}Controller.class);
    
    @Autowired 
    private ${model.model}Service ${model.modelL}Service;

#foreach($field in $fieldList)
#if(${field.inputType}=="select"&&${field.inputValueOne})
	@Autowired 
    private ${field.inputValueOneU}Service ${field.inputValueOneL}Service;

#end
#end
    /**
     * 跳转至列表页面
	 * @author ${model.author} ${date}
     * @return 返回页面以及页面模型
     */
    @RequestMapping("/page")
    public String page(Model model,HttpServletRequest request)throws Exception{
		Map<String, Object> params = HandleMessyCode.handle(ServletsUtil.getParameters(request));
		params.put("sitesCode", getUser().getSitesCode()); 
#foreach($field in $fieldList)
#if(${field.inputType}=="select"&&${field.inputValueOne})
		model.addAttribute("${field.field}", params.get("${field.field}"));
		List<Map<String,Object>> ${field.inputValueOneL}EntityList = ${field.inputValueOneL}Service.findAllByParams(params);
		model.addAttribute("${field.inputValueOneL}EntityList", ${field.inputValueOneL}EntityList);
#end
#end
		return "mis/${model.modelL}List";
	}
    
    
    /**
     * 加载表格数据  
    * @param request
	* @param response
	* @param page
	* @param pageSize
	* @param sortType
	* @param sortOrder
	* @param model
	* @author ${model.author} ${date}
    * @return 查询所得数据
    */
    @RequestMapping(value="/list", method=RequestMethod.GET)
    @ResponseBody
    public SimpleLayUIQueryResult<Map<String, Object>> list(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "page", defaultValue = "0") int page,
			@RequestParam(value = "limit", defaultValue = "30") int pageSize,
			@RequestParam(value = "sort", defaultValue = "CREATE_TIME") String sortType,
			@RequestParam(value = "order", defaultValue = "desc") String sortOrder,
            Model model){
		SimpleLayUIQueryResult<Map<String, Object>> result = new SimpleLayUIQueryResult<Map<String, Object>>();
		try {
			Map<String, Object> params = HandleMessyCode.handle(ServletsUtil.getParameters(request));
			params.put("sitesCode", getUser().getSitesCode());
			CustomRowBounds rowBounds = new CustomRowBounds(page, pageSize, sortType, sortOrder);
			Page<Map<String, Object>> pager = ${model.modelL}Service.selectListPage(params, rowBounds);
			result.setReturnCode(ReturnCode.SUCCESS);
			result.setMessage("数据加载成功");
			result.setCount(pager.getTotalElements());
			result.setData(pager.getContent());
		} catch (Exception e) {
			logger.error("列表查询异常：",e);
			result.setReturnCode(ReturnCode.ERROR);
			result.setMessage("数据加载异常");
		}
		return result;
	}
    
    /**
     * 新增页面
	 * @author ${model.author} ${date}
     * @return 跳转到${model.comment}新增页面
     */
    @RequestMapping("/add")
    public String add(HttpServletRequest request,HttpServletResponse response,Model model){
		try {
			Map<String, Object> params = HandleMessyCode.handle(ServletsUtil.getParameters(request));
			params.put("sitesCode", getUser().getSitesCode()); 
#foreach($field in $fieldList)
#if(${field.inputType}=="select"&&${field.inputValueOne})
			model.addAttribute("${field.field}", params.get("${field.field}"));
			List<Map<String,Object>> ${field.inputValueOneL}EntityList = ${field.inputValueOneL}Service.findAllByParams(params);
			model.addAttribute("${field.inputValueOneL}EntityList", ${field.inputValueOneL}EntityList);
#end
#end
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
        return "mis/${model.modelL}Form";
    }
    
    
    /**       
	* 保存新增 /修改
	* @param request
	* @param response
	* @param entity ${model.comment}
	* @author ${model.author} ${date}
	* @return             
	*/
	@RequestMapping(value = "/saveAdd", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public SimpleObjectResult<${model.model}Entity> saveAdd(HttpServletRequest request,HttpServletResponse response,${model.model}Entity entity) {
		
		SimpleObjectResult<${model.model}Entity> result = new SimpleObjectResult<${model.model}Entity>();
 		try{
 			Map<String, Object> params = HandleMessyCode.handle(ServletsUtil.getParameters(request));
 			 //当前用户
 			params.put("userCode", getUserCode());
			params.put("sitesCode", getUser().getSitesCode());
 			if(!StringUtils.hasText(entity.getCode())){
	 			//执行保存
	 			${model.modelL}Service.save${model.model}Add(params, entity);
 			}else{//执行更新 
 				${model.modelL}Service.save${model.model}Edit(params,entity);
 			}
 			result.setReturnCode(ReturnCode.SUCCESS);
			result.setMessage("保存成功");
			result.setObj(entity);
			
 		}catch(Exception e){
		logger.error(e.getMessage());
			result.setReturnCode(ReturnCode.ERROR);
			result.setMessage("新增异常");
 		}
		return result;
	}
    
    /**
     * 跳转到编辑页面
	 * @author ${model.author} ${date}
     * @param ${model.model}  跳转到编辑页面
     */ 
    @RequestMapping("/edit")
    public String edit(HttpServletRequest request,
			HttpServletResponse response,
			Model model,@RequestParam(value = "code") String code){
		try {
			Map<String, Object> params = HandleMessyCode.handle(ServletsUtil.getParameters(request));
			params.put("sitesCode", getUser().getSitesCode()); 
			params.remove("code");
#foreach($field in $fieldList)
#if(${field.inputType}=="select"&&${field.inputValueOne})
			model.addAttribute("${field.field}", params.get("${field.field}"));
			List<Map<String,Object>> ${field.inputValueOneL}EntityList = ${field.inputValueOneL}Service.findAllByParams(params);
			model.addAttribute("${field.inputValueOneL}EntityList", ${field.inputValueOneL}EntityList);
#end
#end
			${model.model}Entity entity = ${model.modelL}Service.findByCode(code);
			model.addAttribute("${model.modelL}", entity);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return "mis/${model.modelL}Form";
	}
     
    /**
     * 跳转至详细信息页面
     * @param ${model.modelL} 参数
	 * @author ${model.author} ${date}
     * @return 详情数据
     */ 
   @RequestMapping(value = "/detail", method = RequestMethod.GET)
	public String toDetail(HttpServletRequest request,HttpServletResponse response,Model model){
		try {
			Map<String, Object> params = ServletsUtil.getParameters(request);
			String code = (String)params.get("code"); 
			List<Map<String,Object>> ${model.modelL}Entity = ${model.modelL}Service.findAllByParams(params);
			model.addAttribute("${model.modelL}Entity", ${model.modelL}Entity.get(0));
		} catch (Exception e) {
			logger.error("exception", e);
			return "404";
		} 
		return "mis/${model.modelL}Info";
	}
    
    	/**       
	* 禁用-逻辑删除  
	* @author ${model.author} ${date}
	* @param request
	* @param response
	* @return             
	*/
	@RequestMapping(value = "/delete", method =  RequestMethod.POST )
	@ResponseBody
	public SimpleObjectResult<Map<String, Object>> delete${model.model}(HttpServletRequest request,
			HttpServletResponse response) {
		SimpleObjectResult<Map<String, Object>> result = new SimpleObjectResult<Map<String, Object>>();
		try {
			Map<String, Object> params = ServletsUtil.getParameters(request);
			
			boolean flag = ${model.modelL}Service.delete${model.model}ByCode((String)params.get("code"));
			if(flag){
				result.setReturnCode(ReturnCode.SUCCESS);
				result.setMessage("删除成功！");
			}else{
				result.setReturnCode(ReturnCode.ERROR);
				result.setMessage("删除失败！");
			}
		}catch(Exception e){
		logger.error(e.getMessage());
			result.setReturnCode(ReturnCode.ERROR);
			result.setMessage("删除失败!");
 		}
		return result;
	}
    
  
}